// pages/load/reset/reset.js
var api = require('../../../app/app.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    registeInfo: '',
    tel: '',
    reset_tel: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/tel.jpg',
    reset_code: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/code.jpg',
    reset_password: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/password.jpg',
    reset_newPassword: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/newPassword.jpg',
    sendTime: '发送验证码',
    sendColor: '#61CD82',
    time: 60,
    disabled: false,
    password: '',
    newPassword: '',
    phone: '',
    tipsStyle: 'none',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '重置密码'
    })
  },
  // toast弹框方法抽取
  toast: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 1500,
      mask: true
    })
  },
  telClick: function(e) { // 获取手机号
    this.setData({
      tel: e.detail.value,
      tipsStyle: 'none'
    })
    var that = this
    var length = that.data.tel.length
    if (length == 11) {
      console.log(111113)
      wx.request({ // 检测手机号是否存在
        url: api.Checkiphone,
        data: {
          tel: that.data.tel,
        },
        method: "POST",
        header: {
          'content-type': "application/x-www-form-urlencoded"
        },
        success(res) {
          // console.log(res.data);
          if (res.data.re == 'succ') {
            that.setData({
              tipsStyle: 'none',
              disabled: false
            })
          } else if (res.data.re == 'fail') {
            that.setData({
              tipsStyle: 'block',
              disabled: true
            })
          } 
        }
      })
    } else {}
  },
  passwordClick: function (e) { // 获取新密码
    this.setData({
      password: e.detail.value
    })
  },
  newPasswordClick: function (e) { // 获取新密码一号
    this.setData({
      newPassword: e.detail.value
    })
  },
  sendCode: function() { // 点击获取验证码
    var that = this;
    if (this.data.tel === '') { // 手机号为空时候
      this.toast('请输入手机号')
      return
    }
    if (!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(this.data.tel))) { // 判断是否是手机号
      this.toast('手机号输入错误');
      return;
    } else {
      this.toast('获取验证码成功');
      // 倒计时60秒
      var currentTime = that.data.time;
      that.setData({
        sendTime: currentTime + '秒重新获取',
        disabled: true,
        sendColor: '#B7B4B4'
      })
      var interval = setInterval(function () {
        that.setData({
          sendTime: (currentTime - 1) + '秒重新获取',
          disabled: true,
          sendColor: '#B7B4B4'
        })
        currentTime--
        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            sendTime: '重新获取',
            time: 60,
            disabled: false,
            sendColor: '#F0854B'
          })
        }
      }, 1000)
      // 提交手机号给后台获取验证码
      wx.request({
        url: api.RegisteCode,
        data: {
          phone: this.data.tel,
        },
        method: "POST",
        header: {
          'content-type': "application/x-www-form-urlencoded"
        },
        success(res) {
          console.log(res.data);
          if (res.data.re == 'succ') {
            that.toast('获取验证码成功');
          } else if (res.data.re == 'fail') {
            that.toast('获取验证码失败，请重新获取');
          }
        }
      })
    }
  },
  sumbitReset: function(e) { // 提交表单
    if (!(e.detail.value.tel && e.detail.value.code && e.detail.value.password && e.detail.value.newPassword)) {
      wx.showModal({
        title: '提示',
        content: '请填写对应信息',
        success(res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
    } else if (this.data.password !== this.data.newPassword) {
          this.toast('俩次密码不相同')
      } else {
        var that = this;
        var userInfo = { // 获取用户信息
          tel: e.detail.value.tel,
          code: e.detail.value.code,
          password: e.detail.value.password
        }
        var that = this
        wx.request({ // 传数据给后台
          url: api.Reset,
          method: 'POST',
          data: userInfo,
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res)
            if (res.data.re == 'succ') {
              wx.showModal({
                title: '重置密码成功',
                content: '快去登录小程序吧！',
                success(res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '../login/login',
                    })
                  } else if (res.cancel) {}
                }
              })
              that.setData({
                resetInfo: ''
              })
            } else if (res.data.re == 'fail') {
              that.toast('重置密码失败！')
            } else if (res.data.re == 'diff') {
              that.toast('验证码错误！')
            }
          }
        })
      }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})